package com.songmy.spider.run.javaspider;

import javax.management.JMException;






import com.songmy.spider.run.my.CallBackPipeline;
import com.songmy.spider.run.my.UsuallySpiderProcessor;
import com.songmy.spider.run.vo.SpiderRule;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.monitor.SpiderMonitor;
import us.codecraft.webmagic.scheduler.FileCacheQueueScheduler;
import us.codecraft.webmagic.scheduler.RedisScheduler;
import us.codecraft.webmagic.scheduler.Scheduler;

/***
 * 爬虫入口
 * @author Marc
 * @version 1.0
 */
public class MySpider {
	
	private MySpider() {
	}
	
	
	public static Spider start(SpiderRule rule) throws Exception{
		
		
		Spider s = Spider.create(new UsuallySpiderProcessor(rule))
				.thread(rule.getThread())
				.setDownloader(new HttpClientDownloader())
				.addPipeline(new CallBackPipeline(rule))
				.addUrl(rule.getUrls().toArray(new String[0]));
		try {
			switch (rule.getSchedulerType()) {
			case File:
				s.setScheduler(new FileCacheQueueScheduler(rule.getTempPath()));
				break;
			case Redis:
				s.setScheduler(new RedisScheduler(rule.getTempPath()));
				break;
			case OTHER:
				Class<? extends Scheduler> c = (Class<? extends Scheduler>) Class
						.forName(rule.getOtherScheduler());
				Scheduler scheduler = c.newInstance();
				s.setScheduler(scheduler);
				break;
			case Queue:
			default:
				break;
			}
			
			if(rule.getProxyList()!=null && rule.getProxyList().size() > 0){
				s.getSite().setHttpProxyPool(rule.getProxyList(),false);
			}
			
		}  catch (Exception e) {
			throw new RuntimeException(e);
		}
		try {
			SpiderMonitor.instance().register(s);
			s.start();
		} catch (JMException e) {
			throw new RuntimeException(e);
		}
		return s;
	}
	
}
